package com.example.user.myhjjf_app.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.blankj.utilcode.util.ActivityUtils;
import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.model.Model_Tagging;
import com.example.user.myhjjf_app.presenter.Presenter_Tagging;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public  class  Activity_Tagging extends AppCompatActivity {
    Model_Tagging tagging;
    public static Context context;
    @BindView(R.id.mapView)
    MapView mapView;
    @BindView(R.id.bt_fgx)
    Button btFgx;
    @BindView(R.id.bt_gjx)
    Button btGjx;
    @BindView(R.id.bt_zd)
    Button btZd;
    public static BaiduMap baiduMap;
    Model_Tagging.ValueBean.ArrayBean.DetailsBean detailsBean;
    Presenter_Tagging presenter_tagging;
    View view;
    LatLng latLng = null;
    private int Buttonpd=0;
    private String ButtonName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__tagging);
        ButterKnife.bind(this);
        context = getApplicationContext();
        baiduMap = mapView.getMap();
        baiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                    latLng = marker.getPosition();
                    showView(marker.getPosition());
                   return false;
            }
        });
        baiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                Intent intent = null;
                Bundle bundle = new Bundle();
                bundle.putString("lat", String.valueOf(latLng.latitude));
                bundle.putString("lng", String.valueOf(latLng.longitude));
                bundle.putString("type","add");
                if (Buttonpd==1){
                    Buttonpd = 0;
                    if (ButtonName.equals("bt1")){
                        intent = new Intent(Activity_Tagging.this,Acitvity_edit.class);
                    }else if (ButtonName.equals("bt2")){
                        intent = new Intent(Activity_Tagging.this,Activity_gjx.class);
                    }else if (ButtonName.equals("bt3")){
                        intent = new Intent(Activity_Tagging.this,Activity_fgx.class);
                    }
                    intent.putExtras(bundle);
                    startActivityForResult(intent,0);
                }
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });
        init();
    }


    private void init(){
        OkGo.<String>post(HJJF_Url.getHJJF_Url().login( "api/map/forward.do"))
                .tag(this)
                .params("id", Model_Regional.getRegional().getZoneId())
                .params("level", "2")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        tagging = JSON.parseObject(response.body(), Model_Tagging.class);
                        presenter_tagging = new Presenter_Tagging(tagging,getApplicationContext());
                        for (Model_Tagging.ValueBean.LinesBean linesBean : tagging.getValue().getLines()) {
                            addbord(JSON.parseArray(linesBean.getPoints(), Model_Tagging.ValueBean.LatLngBean.class), Color.GREEN, 5);
                        }
                    }
                });
    }

    public void addbord(List<Model_Tagging.ValueBean.LatLngBean> list, int color, int width) {
        ArrayList<LatLng> latLngList = new ArrayList<>();
        for (Model_Tagging.ValueBean.LatLngBean latLngBean : list) {
            latLngList.add(new LatLng(latLngBean.getLat(), latLngBean.getLng()));
        }
        PolylineOptions polylineOptions = new PolylineOptions()
                .width(width)
                .color(color)
                .points(latLngList);
        baiduMap.addOverlay(polylineOptions);
    }



    @OnClick({R.id.bt_fgx, R.id.bt_gjx, R.id.bt_zd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_fgx:
                Toast.makeText(context, "请选择位置", Toast.LENGTH_SHORT).show();
                Buttonpd=1;
                ButtonName = "bt3";
                break;
            case R.id.bt_gjx:
                Toast.makeText(context, "请选择位置", Toast.LENGTH_SHORT).show();
                Buttonpd=1;
                ButtonName = "bt2";
                break;
            case R.id.bt_zd:
                Toast.makeText(context, "请选择位置", Toast.LENGTH_SHORT).show();
                Buttonpd=1;
                ButtonName = "bt1";
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==1){
            init();
            Activity_Tagging.baiduMap.hideInfoWindow();
        }
    }




    public void showView(LatLng latLnddddddg){
        for (int i = 0; i < tagging.getValue().getArray().size(); i++) {
            Double lat =  tagging.getValue().getArray().get(i).getLat();
            Double lng = tagging.getValue().getArray().get(i).getLng();
            if (lat.equals(latLnddddddg.latitude)&&lng.equals(latLnddddddg.longitude)){
                int type = tagging.getValue().getArray().get(i).getType();
                if (type==1){
                    detailsBean = tagging.getValue().getArray().get(i).getDetails();
                    view = LayoutInflater.from(this).inflate(R.layout.listitem_edittagging,null);
                    TextView Name = view.findViewById(R.id.tv_zd_mc);
                    TextView Id = view.findViewById(R.id.tv_zd_bm);
                    TextView Address = view.findViewById(R.id.tv_zd_dz);
                    TextView Measure = view.findViewById(R.id.tv_zd_mj);
                    TextView Distance = view.findViewById(R.id.tv_zd_jldwzd);
                    Name.setText(detailsBean.getName());
                    Id.setText(detailsBean.getCode());
                    Address.setText(detailsBean.getAddress());
                    Measure.setText(String.valueOf(detailsBean.getArea()));
                    Distance.setText(String.valueOf(detailsBean.getDistance()));
                    Button button1 = view.findViewById(R.id.bt_compile);
                    button1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(Activity_Tagging.this,Acitvity_edit.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("detailsBean",detailsBean);
                            bundle.putString("type","edit");
                            intent.putExtras(bundle);
                            startActivityForResult(intent,0);
                        }
                    });
                    Button button = view.findViewById(R.id.bt_search);
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("detailsBean",detailsBean);
                            ActivityUtils.startActivity(bundle,Activity_Tagmas.class);
                        }
                    });
                }else if (type==2){
                    detailsBean = tagging.getValue().getArray().get(i).getDetails();
                    view = LayoutInflater.from(this).inflate(R.layout.listitem_editgjx,null);
                    TextView name = view.findViewById(R.id.tv_gjx_mc);
                    TextView zhdName = view.findViewById(R.id.tv_gjx_zjdmc);
                    TextView zggls = view.findViewById(R.id.tv_gjx_zggls);
                    TextView zjdglcdxs = view.findViewById(R.id.tv_gjx_zjdglcdxs);
                    TextView dxqxcdxs1 = view.findViewById(R.id.tv_gjx_dxqxcdxs1);
                    TextView dxqxcdxs2 = view.findViewById(R.id.tv_gjx_dxqxcdxs2);
                    name.setText(detailsBean.getName());
                    zhdName.setText(detailsBean.getZjdName());
                    zggls.setText(String.valueOf(detailsBean.getZggqCount()));
                    zjdglcdxs.setText(String.valueOf(detailsBean.getZjdCount()));
                    dxqxcdxs1.setText(String.valueOf(detailsBean.getRedCount()));
                    dxqxcdxs2.setText(String.valueOf(detailsBean.getYellowCount()));
                    Button button = view.findViewById(R.id.bt_compile);
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(Activity_Tagging.this,Activity_gjx.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("detailsBean",detailsBean);
                            bundle.putString("type","edit");
                            intent.putExtras(bundle);
                            startActivityForResult(intent,0);
                        }
                    });
                    Button button1 = view.findViewById(R.id.bt_search);
                    button1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("detailsBean",detailsBean);
                            ActivityUtils.startActivity(bundle,Activity_GjxXq.class);
                        }
                    });
                }else if (type==3){

                    detailsBean = tagging.getValue().getArray().get(i).getDetails();
                    view = LayoutInflater.from(this).inflate(R.layout.listiem_editfgx,null);
                    TextView mc = view.findViewById(R.id.tv_fgx_mc);
                    TextView zrr = view.findViewById(R.id.tv_fgx_zrr);
                    TextView cdxs = view.findViewById(R.id.tv_fgx_cdxs);
                    mc.setText(detailsBean.getName());
                    zrr.setText(detailsBean.getZjdName());
                    cdxs.setText(String.valueOf(detailsBean.getZggqCount()));
                    Button button = view.findViewById(R.id.bt_compile);
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(Activity_Tagging.this,Activity_fgx.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("detailsBean",detailsBean);
                            bundle.putString("type","edit");
                            intent.putExtras(bundle);
                            startActivityForResult(intent,0);
                        }
                    });
                }
                ImageView imageView = view.findViewById(R.id.marker_close);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Activity_Tagging.baiduMap.hideInfoWindow();//隐藏气泡
                    }
                });
                Activity_Tagging.baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(latLnddddddg));
                Activity_Tagging.baiduMap.showInfoWindow(new InfoWindow(view,latLnddddddg,1));
                return;
            }
        }
    }
}
